package com.sjsu.cloudmanager.dao;

import com.sjsu.cloudmanager.model.CloudUser;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.List;

/**
 * Cloud user implementation
 * User: ckempaiah
 * Date: 10/16/11
 * Time: 11:41 PM
 * To change this template use File | Settings | File Templates.
 */
public class CloudUserDaoImpl extends HibernateDaoSupport implements CloudUserDao {

    @Override
    public CloudUser getCloudUser(Integer userId) {
        return getHibernateTemplate().get(CloudUser.class, userId);
    }

    @Override
    public CloudUser getCloudUser(String userName) {
        List result = getHibernateTemplate().find("from CloudUser where userName=?", userName);
        if (CollectionUtils.isNotEmpty(result)) {
            return (CloudUser)result.get(0);
        }
        return null;
    }

    @Override
    public void addUpdateCloudUser(CloudUser cloudUser) {
        getHibernateTemplate().saveOrUpdate(cloudUser);
    }

    @Override
    public List<CloudUser> findCloudUserList() {
        return getHibernateTemplate().find("from CloudUser where administrator=?", false);
    }
}
